function Tests = datesTest()
Tests = functiontests(localfunctions) ;
end


%**************************************************************************


function testDat2str(This) %#ok<*DEFNU>
actual = dat2char(yy(2010)) ;
expected = '2010Y' ;
assertEqual(This, actual, expected) ;

actual = dat2char(qq(2010,1)) ;
expected = '2010Q1' ;
assertEqual(This, actual, expected) ;

actual = dat2char(qq(2010,1)) ;
expected = '2010Q1' ;
assertEqual(This, actual, expected) ;

actual = dat2char(qq(2010,1), ...
    'dateFormat=','YYYY-MM-EE') ;
expected = '2010-01-31' ;
assertEqual(This, actual, expected) ;

actual = dat2char(qq(2010,1), ...
    'dateFormat=','YYYY-MM-WW') ;
expected = '2010-01-29' ;
assertEqual(This, actual, expected) ;

actual = dat2char(mm(2010,1)) ;
expected = '2010M01' ;
assertEqual(This, actual, expected) ;

actual = dat2char(ww(2002,1)) ;
expected = '2002W01';
assertEqual(This, actual, expected) ;

actual = dat2char(ww(2002,1), ...
    'dateFormat=','YYYY-MM') ;
expected = '2002-01';
assertEqual(This, actual, expected) ;

actual = dat2char(ww(2002,1), ...
    'dateFormat=','$YYYY-MM-DD') ;
expected = '2002-01-03';
assertEqual(This, actual, expected) ;

actual = dat2char(dd(2002,1,1), ...
    'dateFormat=','$YYYY-Mmm-DD') ;
expected = '2002-Jan-01';
assertEqual(This, actual, expected) ;
end % testDat2str()


%**************************************************************************


function testStr2dat(This)
actual = dat2char(str2dat('01-2010', ...
    'dateFormat=', 'MM-YYYY', 'freq=', 12)) ;
expected = '2010M01' ;
assertEqual(This, actual, expected) ;

actual = dat2char(str2dat('01-2010', ...
    'dateFormat=', 'MM-YYYY', 'freq=', 4)) ;
expected = '2010Q1' ; 
assertEqual(This, actual, expected) ;

actual = dat2char(str2dat('01-2010', ...
    'dateFormat=', 'MM-YYYY', 'freq=', 1)) ;
expected = '2010Y' ;
assertEqual(This, actual, expected) ;

actual = dat2char(str2dat('2001-12-31', ...
    'dateFormat=', '$YYYY-MM-DD','freq=', 52)) ;
expected = '2002W01';
assertEqual(This, actual, expected) ;
end


%**************************************************************************


function testStr2datMonths(This)
monthList = irisget('months');
for i = 1 : 12
    m = monthList{i}(1:3);
    
    % Monthly frequency.
    actual = dat2char(str2dat(['01-',m,'-2010'], ...
        'dateFormat=', 'DD-Mmm-YYYY', 'freq=', 12)) ;
    expected = sprintf('2010M%02.0f',i) ;
    assertEqual(This, actual, expected) ;
    
    % Daily frequency.
    actual = dat2char(str2dat(['12-',m,'-2010'], ...
        'dateFormat=', '$DD-Mmm-YYYY')) ;    
    expected = sprintf('2010-%s-12',m);
    assertEqual(This, actual, expected) ;
end
end % testStd2datMonths()


%**************************************************************************


function testDat2ttrend(This)
ry = yy(1990) : yy(2100);
rh = hh(1990,1) : hh(2100,1);
rq = qq(1990,1) : qq(2100,1);
rb = bb(1990,1) : bb(2100,1);
rm = mm(1990,1) : mm(2100,1);
for b = 1980 : 10 : 2110
    ty = dat2ttrend(ry,b);
    th = dat2ttrend(rh,b);
    tq = dat2ttrend(rq,b);
    tb = dat2ttrend(rb,b);
    tm = dat2ttrend(rm,b);
    dy = diff(ty);
    dh = diff(th);
    dq = diff(tq);
    db = diff(tb);
    dm = diff(tm);
    assertEqual(This,dy,ones(size(dy)));
    assertEqual(This,dh,ones(size(dh)));
    assertEqual(This,dq,ones(size(dq)));
    assertEqual(This,db,ones(size(db)));
    assertEqual(This,dm,ones(size(dm)));
    if b == 1990
        assertEqual(This,ty(1),0);
        assertEqual(This,th(1),0);
        assertEqual(This,tq(1),0);
        assertEqual(This,tb(1),0);
        assertEqual(This,tm(1),0);
    end
    if b == 2100
        assertEqual(This,ty(end),0);
        assertEqual(This,th(end),0);
        assertEqual(This,tq(end),0);
        assertEqual(This,tb(end),0);
        assertEqual(This,tm(end),0);
    end
end
end % testDat2ttrend()
